ColdFusion Article

Caching Queries to Disk or to Memory with ColdFusion


Table of Contents

  1. Introduction
  2. A Primer on ColdFusion's Built-in Query Caching
  3. A New Method for Caching Queries
  4. Caution When Caching to Disk
  5. Considerations and Conclusion

Considerations

In writing this article, I tested a query that selected the top 1,000 records from a users table containing over 300,000 records while performing many nested inner/left joins to other large database tables. The average execution time without any caching was 12,000 milliseconds (12 seconds). After applying the cf_extremeQueryCache tag to this same query, the average execution time for retrieving this query from the cache was only 600 milliseconds (0.6 seconds). That’s 20 times faster than executing the query without caching. To ColdFusion’s credit, the average execution time for retrieving the cached query when using the built-in cachedWithin method was only 5 milliseconds (0.005 seconds). That’s 2,400 times faster than executing the query normally, and even 120 times faster than using the cf_extremeQueryCache custom tag – as expected since memory access will always be faster than disk access. But, as I explained earlier, the whole point of using this custom tag-based caching solution is to break the limitations of available server memory and the Maximum number of cached queries setting encountered when caching queries with ColdFusion’s built-in caching methods.

Even if you cache a query with an average execution time of four seconds for a 60-second interval, that’s 60 seconds during which the average execution time is likely to be only 0.2 seconds. Anywhere that you can improve the execution time helps in the long run; these improvements magnify as your application receives more traffic and more simultaneous users—every little bit counts.

Conclusion

While ColdFusion natively provides a very effective means of caching queries, there are a number of limitations inherent with this method of query caching. For developers looking to take query caching to the next level and cache higher volumes of queries, queries with larger recordsets, and queries with numerous input parameters with wide variance of resulting recordsets, ColdFusion’s built-in caching methods have limitations. With the cf_extremeQueryCache tag, these limitations are no longer a factor. Whether you’re running a simple query of state names and abbreviations or a complex report evaluating tens of thousands of records, the cf_extremeQueryCache tag can substantially increase performance, contributing to an overall improvement in the speed and reliability of your application.